package com.jr.controller;


import com.google.gson.Gson;
import com.jr.entity.Job;
import com.jr.entity.User;
import com.jr.entity.UserJobEnterprise;
import com.jr.service.JobService;
import com.jr.service.UserJobEnterpriseService;
import com.jr.util.ConverterDate;
import com.jr.util.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户工作企业三表关联
 *
 * @author Snoopy
 * @since 1.0.0 2022-12-31
 */
@Controller
@RequestMapping("/userjobenterprise/")
@SuppressWarnings("all")
public class UserJobEnterpriseController {
    @Autowired
    private JobService jobService;
    @Autowired
    private UserJobEnterpriseService userJobEnterpriseService;

    /**
     * @description: 查询所有所有企业和所有工作信息
     * @author: 张杨
     * @date: 2023/1/3 18:19
     * @param: []
     * @return: java.lang.String
     **/
    @GetMapping(value = "getjobs",produces = "application/json;charset=utf-8")
    public @ResponseBody PageHelper selectjobs(Integer indexpage, String keyword, HttpSession session){
        User user =(User) session.getAttribute("user");
        String uid = null;
        Map map=new HashMap<String,String>();
        if(user!=null){
            uid = user.getUid();
        }
        PageHelper<UserJobEnterprise> pageHelper=new PageHelper<UserJobEnterprise>();
        if (indexpage != null){
            pageHelper.setIndexpage(indexpage);
        }
        if(indexpage==-20){
            map.put("indexpage",null);
            map.put("showcount",null);
            map.put("uid",uid);
            map.put("keyword","");
        }else {
            map.put("indexpage",(pageHelper.getIndexpage()-1)*pageHelper.getShowcount());
            map.put("showcount",pageHelper.getShowcount());
            map.put("uid",uid);
            map.put("keyword",keyword);
        }
        pageHelper.setCountnum(userJobEnterpriseService.getTotalRows(map));
        int countpage = userJobEnterpriseService.getTotalRows(map) % pageHelper.getShowcount()==0 ?
                userJobEnterpriseService.getTotalRows(map) / pageHelper.getShowcount() :
                userJobEnterpriseService.getTotalRows(map) / pageHelper.getShowcount()+1;
        pageHelper.setCountpage(countpage);

        List<UserJobEnterprise> alljob = userJobEnterpriseService.getAllJob(map);

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (UserJobEnterprise userJobEnterprise:alljob){
            userJobEnterprise.getJob().setJrefreshdatestr(simpleDateFormat.format(userJobEnterprise.getJob().getJrefreshdate()));
        }
        pageHelper.setIndexinfos(alljob);
        return pageHelper;
    }
    /**
     * @description: 根据用户Id查询所有企业和所有工作信息
     * @author: 张杨
     * @date: 2023/1/12 10:04
     * @param: [indexpage, keyword, session]
     * @return: com.jr.util.PageHelper
     **/
    @GetMapping(value = "getjob",produces = "application/json;charset=utf-8")
    @SuppressWarnings("all")
    public @ResponseBody
    PageHelper selectjob(Integer indexpage, String keyword, HttpSession session) {
        User user = (User) session.getAttribute("user");
        PageHelper<UserJobEnterprise> pageHelper = new PageHelper<UserJobEnterprise>();
        if (indexpage != null) {
            pageHelper.setIndexpage(indexpage);
        }
        Map map = new HashMap<String, String>();
        map.put("indexpage", (pageHelper.getIndexpage() - 1) * pageHelper.getShowcount());
        map.put("showcount", pageHelper.getShowcount());
        map.put("uid", user.getUid());
        map.put("keyword", keyword);
        pageHelper.setCountnum(userJobEnterpriseService.getTotal(map));
        int countpage = userJobEnterpriseService.getTotal(map) % pageHelper.getShowcount() == 0 ?
                userJobEnterpriseService.getTotal(map) / pageHelper.getShowcount() :
                userJobEnterpriseService.getTotal(map) / pageHelper.getShowcount() + 1;
        pageHelper.setCountpage(countpage);
        List<UserJobEnterprise> alljob = userJobEnterpriseService.getAllJobbyuid(map);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (UserJobEnterprise userJobEnterprise : alljob) {
            userJobEnterprise.getJob().setJrefreshdatestr(simpleDateFormat.format(userJobEnterprise.getJob().getJrefreshdate()));
        }
        pageHelper.setIndexinfos(alljob);
        return pageHelper;
    }
    /**
     * @description: 根据用户Id查询申请状态以及修改申请状态
     * @author: 张杨
     * @date: 2023/1/12 10:07
     * @param: [userJobEnterprise, session]
     * @return: void
     **/
    @GetMapping("addapplication")
    public void addapplication(UserJobEnterprise userJobEnterprise,HttpSession session){
        User user = (User) session.getAttribute("user");
        userJobEnterprise.setUid(user.getUid());
        if(userJobEnterpriseService.getUserJobEnterprise(userJobEnterprise)==null){
            userJobEnterprise.setJobuserattention(null);
            userJobEnterprise.setUpid(String.valueOf(new Date().getTime()));
            userJobEnterpriseService.addApplication(userJobEnterprise);
        }

        boolean boo=userJobEnterpriseService.updateApplication(userJobEnterprise);
    }
    /**
     * @description: 根据用户Id查询关注状态以及修改关注状态
     * @author: 张杨
     * @date: 2023/1/12 10:11
     * @param: [userJobEnterprise, session]
     * @return: void
     **/
    @GetMapping("modifiedstate")
    public void modifiedstate(UserJobEnterprise userJobEnterprise,HttpSession session){
        User user = (User) session.getAttribute("user");
        userJobEnterprise.setUid(user.getUid());
        if(userJobEnterpriseService.getUserJobEnterprise(userJobEnterprise)==null){
            userJobEnterprise.setJobuserstatus(null);
            userJobEnterprise.setUpid(String.valueOf(new Date().getTime()));
            userJobEnterpriseService.addApplication(userJobEnterprise);
        }

        boolean boo=userJobEnterpriseService.modifyEnterpriseAttention(userJobEnterprise);
    }
    /**
     * @description: 根据Jid查询所有工作信息（同步请求）
     * @author: 张杨
     * @date: 2023/1/12 10:12
     * @param: [userJobEnterprise, session, modelAndView]
     * @return: org.springframework.web.servlet.ModelAndView
     **/
    @GetMapping("getjobbyjid")
    public ModelAndView getJobByJid(UserJobEnterprise userJobEnterprise, HttpSession session, ModelAndView modelAndView){
        User user = (User) session.getAttribute("user");
        Map<String,String> map = new HashMap();
        map.put("uid",user.getUid());
        map.put("jid",userJobEnterprise.getJid());
        userJobEnterprise.setUid(user.getUid());

        if(userJobEnterpriseService.getUserJobEnterprise(userJobEnterprise)==null){
            userJobEnterprise.setJobuserattention(null);
            userJobEnterprise.setJobuserstatus(null);
            userJobEnterprise.setUpid(String.valueOf(new Date().getTime()));
            userJobEnterpriseService.addApplication(userJobEnterprise);
        }

        userJobEnterprise = userJobEnterpriseService.selectJobByJid(map);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        userJobEnterprise.getJob().setJrefreshdatestr(simpleDateFormat.format(userJobEnterprise.getJob().getJrefreshdate()));
        modelAndView.addObject("userJobEnterprise",userJobEnterprise);
        modelAndView.setViewName("job_info.jsp");
        return modelAndView;
    }
    /**
     * @description: 根据Jid查询所有工作信息（异步请求）
     * @author: 张杨
     * @date: 2023/1/12 10:28
     * @param: [userJobEnterprise, session]
     * @return: com.jr.entity.UserJobEnterprise
     **/
    @GetMapping(value = "getjobbyeidandjid",produces = "application/json;charset=utf-8")
    public @ResponseBody UserJobEnterprise getJobByEidAndJid(UserJobEnterprise userJobEnterprise, HttpSession session){
        User user = (User) session.getAttribute("user");
        Map<String,String> map = new HashMap();
        map.put("uid",user.getUid());
        map.put("jid",userJobEnterprise.getJid());
        userJobEnterprise.setUid(user.getUid());


        if(userJobEnterpriseService.getUserJobEnterprise(userJobEnterprise)==null){
            userJobEnterprise.setJobuserattention(null);
            userJobEnterprise.setJobuserstatus(null);
            userJobEnterprise.setUpid(String.valueOf(new Date().getTime()));
            userJobEnterpriseService.addApplication(userJobEnterprise);
        }

        userJobEnterprise = userJobEnterpriseService.selectJobByJid(map);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        userJobEnterprise.getJob().setJrefreshdatestr(simpleDateFormat.format(userJobEnterprise.getJob().getJrefreshdate()));
        return userJobEnterprise;
    }

}
